#!/usr/bin/env python
# -*- coding: utf-8 -*-

import ConfigParser
import os

class ConfigException(Exception):
    """A simple exception class used for configuration exceptions"""
    pass

#SEC_PWD_PATH = 'pwd_path'
#SEC_THREADING = 'threading'

PWD_CONFIG_SEC = ['pwd_path', 'threading']

OPT_RAW_DATA_PATH = 'raw_data_dir'
OPT_LOGDIR = 'log_dir'
OPT_OUTPUT = 'out_dir'
OPT_PW_RES_DICT = 'pw_res_dict'
OPT_KW_DICT = 'kw_dict'
#
OPT_PRODUCER_NUM = 'processor'
OPT_WORK_NUM = 'fetcher_num'

PWD_OPTIONS = [OPT_RAW_DATA_PATH, OPT_PW_RES_DICT, OPT_KW_DICT, OPT_LOGDIR, OPT_OUTPUT, \
               OPT_PRODUCER_NUM, OPT_WORK_NUM]
#
class Config:
    
    def __init__(self, cfg, exec_base):       
        self._options = {}
        self.__load_all_opt(cfg)
        self._exec_base = exec_base
        
    def __load_all_opt(self, cfg):
        
        for sec in PWD_CONFIG_SEC:
            ll = cfg.items(sec)
            for (opt, val) in ll:
                self._options[opt] = val
    #
    def get_opt(self, opt):
        try:
            val = self._options[opt]
        except KeyError, e:
            val = None
        return val
    
    def get_execbase(self):
        return self._exec_base

def load_config(config, exec_base = None):
    fd = open (config, "r")
    c = ConfigParser.ConfigParser()
    c.readfp(fd)
    
    cfg = Config(c, exec_base)
    return cfg


if __name__ == '__main__':
    cofig_file = './pwd_cert.conf'
    cfg = load_config(cofig_file)
        
    for opt in PWD_OPTIONS:
        val = cfg.get_opt(opt)
        print type(val)
        print 'Option: %s; Value: %s' %(opt, val)        
